tools_SQL__mariadb_connect(){
    unset root_pass
    input "请输入密码[ root ]:" root_pass
    if [[ -z "$root_pass" ]];then
        red_bg "未输入密码"
        exit 1
    fi
    input "请输入数据库服务IP地址[默认本机]" db_host
    input_check "$db_host" "数据库主机IP地址" "localhost" "db_host"
    red_z "正在检测网络连接" 
    ping $db_host -c 5 
    chek_status "$?" "网络连接检测"
    if [[ "$db_host" -eq 'localhost' ]];then
        sql_cmd="mysql -uroot -p$root_pass -e "
    else
        sql_cmd="mysql -uroot -p$root_pass -h$db_host -e "
    fi
    $sql_cmd "show databases;"
    chek_status "$?" "数据库连接测试" "q"
}
tools_SQL__mariadb_force_set_passwd(){
    input "请输入新密码,默认:[ zuogobjiao178 ]" new_passwd
    input_check "$new_passwd" "需要强制设置的root密码" "zuogobjiao178" "new_passwd"
    service_stop 'mariadb'
    mysqld_safe --skip-grant-tables &
}
tools_SQL__mariadb_add_db(){
    tools_SQL__mariadb_connect #检测连接
    status='true'
    while $status;do
        if [[ -z "$1" ]];then
            input "请输入需要添加的数据库" "db_name"
        else
            db_name=$1
        fi
        $sql_cmd "create database ${db_name} character set utf8 collate utf8_bin;" 
        if [[ $? == 0 ]];then
            red_bg "找不到该用户: [ $db_name ]"
            input '重新输入?[ y/n ]' 'reput'
            if [[ "$reput" == 'y' ]];then
                status='true'
            else
                status=''
            fi
        fi
    done 
}
tools_SQL__mariadb_del_user(){
    tools_SQL__mariadb_connect #检测连接
    status='true'
    while $status;do
        if [[ -z "$1" ]];then
            input "请输入需要删除的用户名" "db_user"
        else
            db_user=$1
        fi
        hosts=`$sql_cmd "select user,host from mysql.user;" | grep ${db_user} | awk '{print $2}' | wc -l`
        if [[ $hosts == 0 ]];then
            red_bg "找不到该用户: [ $db_user ]"
            input '重新输入?[ y/n ]' 'reput'
            if [[ "$reput" == 'y' ]];then
                status='true'
            else
                status=''
            fi
        fi
    done 
}
tools_SQL__mariadb_add_user(){
    tools_SQL__mariadb_connect
    status='true'
    while $status;do
        input "请输入需要添加的用户名" db_user
        if [[ -z "$db_user" ]];then
            echo -e "未检测到输入,请重试"
            status='true'
        else
            input "请输入新用户密码" db_pass
            if [[ -z "$db_pass" ]];then
                echo -e "未检测到输入,请重试"
                status=true
            else
                input "请输入该用户可操作的数据库[默认: $db_user]" db_name
                if [[ -z "$db_name" ]];then
                    db_name=$db_user
                fi
                input "请输入该用户可登录的主机/范围,默认[ localhost ]" db_host
                if [[ -z "$db_host" ]];then
                    db_host='localhost'
                fi
                $sql_cmd "create user  $db_user;"
                $sql_cmd "create database  $db_name;"
                chek_status "$?" "创建用户: [ $db_user ]"
                $sql_cmd  "grant all privileges on ${db_name}.* to '${db_user}'@'${db_host}' identified by '${db_pass}';"
                chek_status "$?" "数据库用户创建"
                green_zt "当前数据库列表信息如下："
                $sql_cmd "show databases;"
                green_zt "当前数据库用户列表如下:"
                $sql_cmd "select user from mysql.user;"
                red_z_green_z "用户添加成功:" "[ $db_user ]"
                mkdir -p /info/
                echo -e "用户名: [ $db_user ];数据库: [ $db_name ];密码: [ $db_pass ]" >> /info/db_info.conf
                input "是否继续添加[y/n],默认[ n ]" add
                if [[ "$add" == 'y' || "$add" == "Y" ]];then
                    status='true'
                else
                    status=''
                fi
                echo "$add"
                read adds
            fi
        fi
    done
    red_z_green_z "数据库用户添加完成" "信息已记录在: [ /info/db_info.conf ]文件"
    red_z_green_z "请记得在查看之后删除该文件" "命令：[ rm -f /info/db_info.conf ]"
}